from bilibliwebutils import *
import multiprocessing
import requests
import time

mp_data_path = "./data_mp/"

def process_task(url):
    # print("《《" * 10 + url + "》》》" * 10)
    resp = requests.get(url)
    items = parse_rank_html(resp.text, True)
    file_path = mp_data_path + url.split("/")[-1] + ".txt"
    write_local_data(items, file_path)

def mutil_process():
    """
    创建当前系统最大并发数的进程池
    :return:
    """
    pool = multiprocessing.Pool(multiprocessing.cpu_count())
    for api in B_RANK_APIS:
        pool.apply_async(process_task, (B_RANK_URL + api, ))
    pool.close()
    pool.join()


if __name__ == "__main__":
    create_floder(mp_data_path)
    start = time.time()
    mutil_process()
    end = time.time()
    print("\n 》》》mutil thread cost:" + str(end - start) + " seconds")